//
//  Huo15TestModule.m
//  Huo15-WifiModule
//
//  Created by 赵博 on 2023/11/28.
//


#import "Huo15TestModule.h"

@implementation Huo15TestModule
UNI_EXPORT_METHOD(@selector(add:callback:))
-(void)add:(NSDictionary*)json callback:(UniModuleKeepAliveCallback)callback{
    int a,b;
    if ([json[@"a"] isKindOfClass:[NSNumber class]]) {
        a = [json[@"a"] intValue];
        // 现在你可以使用变量 'a'
    } else {
        // 处理 'a' 不是NSNumber的情况
        NSLog(@"a 不能为空");
        return;
    }
    if ([json[@"a"] isKindOfClass:[NSNumber class]]) {
        b = [json[@"b"] intValue];
        // 现在你可以使用变量 'a'
    } else {
        // 处理 'a' 不是NSNumber的情况
        NSLog(@"b 不能为空");
        return;
    }
    
    
    if (callback) {
        callback(@{
            @"code":@(0),
            @"result":@(a+b)
        },NO);
    }
    
    
}
UNI_EXPORT_METHOD(@selector(requestPermissions))
-(void)requestPermissions{
    NSLog(@"requestPermissions");
}
UNI_EXPORT_METHOD(@selector(connectWifi:))
- (void)connectWifi:(NSDictionary *)json{
    NSString*ssid,*password;
    NSLog(@"当前ssid:%@",[self currentWifiSSID]);
    if ([json[@"ssid"] isKindOfClass:[NSString class]]) {
        ssid = [json[@"ssid"] stringValue];
        // 现在你可以使用变量 'ssid'
    } else {
        // 处理 'ssid' 不是NSNumber的情况
        NSLog(@"ssid 不能为空");
        NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:@"ssid 不能为空",@"msg",nil];
        NSString * eventName = @"huo15_notify";
        DCUniSDKInstance * instance = self.uniInstance;
        [instance fireGlobalEvent:eventName params:dict];
        return;
    }
    if ([json[@"password"] isKindOfClass:[NSString class]]) {
        password = [json[@"password"] stringValue];
        // 现在你可以使用变量 'password'
    } else {
        // 处理 'password' 不是NSNumber的情况
        NSLog(@"password 不能为空");
        NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:@"password 不能为空",@"msg",nil];
        NSString * eventName = @"huo15_notify";
        DCUniSDKInstance * instance = self.uniInstance;
        [instance fireGlobalEvent:eventName params:dict];
        return;
    }
    
    NEHotspotConfiguration *configuration=[[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:password isWEP:NO];
    [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Failed to connect to WiFi ssid,%@: %@",ssid, [error localizedDescription]);
            NSString *msg=[NSString stringWithFormat:@"链接 WiFi 失败，ssid,%@: %@",ssid,[error localizedDescription]];
            NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:msg,@"msg",@false,@"is_connect",nil];
            NSString * eventName = @"huo15_notify";
            DCUniSDKInstance * instance = self.uniInstance;
            [instance fireGlobalEvent:eventName params:dict];
        } else {
            NSLog(@"Successfully connected to WiFi");
            
            NSString *msg=[NSString stringWithFormat:@"链接WiFi成功！ ssid:%@",ssid];
            NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:msg,@"msg",@true,@"is_connect",nil];
            NSString * eventName = @"huo15_notify";
            DCUniSDKInstance * instance = self.uniInstance;
            [instance fireGlobalEvent:eventName params:dict];
        }
    }];
    
}
- (NSString *)currentWifiSSID {
    NSString *ssid = nil;
    CFArrayRef interfaces = CNCopySupportedInterfaces();
    if (interfaces) {
        CFDictionaryRef info = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));
        if (info) {
            ssid = CFDictionaryGetValue(info, kCNNetworkInfoKeySSID);
            CFRelease(info);
        }
        CFRelease(interfaces);
    }
    return ssid;
}

@end
